package us.gaaoc.framework.signature.digital.ldap;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import us.efiling.gaje2.conf.LdapSettings;




import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;

/**
 * 
 * @author hea
 * @date Feb 9, 2006
 * @version: GAJE 1.5
 * 
 */
@Configurable
public class LdapClient {
	
	LdapSettings settings;
	
	@Autowired
	public void setSettings(LdapSettings settings) {
		this.settings = settings;
	}
	
	private int  ldapVersion, searchScope;
	
	Logger log = Logger.getLogger(LdapClient.class);

	
	public LdapClient(){
		/*
		//working from office then open following comments
		ldapPort = LDAPConnection.DEFAULT_PORT;
	//	ldapHost = "ldap.gaje.us";		
		ldapPort = 389;
//		// if working from home open the comments
//		ldapPort = 389;
//		ldapPort = LDAPConnection.DEFAULT_SSL_PORT;
    	ldapHost = "localhost";
		loginDN = "cn=Manager,DC=gaje,DC=us";
		loginPassword = "secret";
		*/
		searchScope = LDAPConnection.SCOPE_SUB;
		ldapVersion = LDAPConnection.LDAP_V3;
		
	}

	public InputStream retreiveUserCertificate(String userCN) {
		ByteArrayInputStream certificate = null;

		// Container base to search
		String searchBase = "DC=gaje,DC=us";
		String filter = "(&(objectClass=pkiUser)(cn=" + userCN +"))"; 

		LDAPConnection ldapConnection = new LDAPConnection();

		try {
			// Connect to the server
			ldapConnection.connect(settings.getHost(), settings.getPort());

			// bind to the server
			ldapConnection.bind(ldapVersion, settings.getLoginDn(), settings.getLoginPassword().getBytes("UTF8"));

			// perform search
			LDAPSearchResults searchResult = ldapConnection.search(searchBase, searchScope, filter, null, false);

			if (searchResult.hasMore()){
				LDAPEntry nextEntry = searchResult.next();
				LDAPAttribute userCertificateName = nextEntry.getAttribute("userPKCS12");
				if (userCertificateName != null) {
					byte[] certBytes = userCertificateName.getByteValue();
					certificate = new ByteArrayInputStream(certBytes);
				}
			}

		} catch (LDAPException e) {
			log.error("Error: " + e.toString(),e);			
		} catch (UnsupportedEncodingException e) {
			log.error("Error: " + e.toString(),e);	
		}  
		
		return certificate;
	}
	
	public Image retrieveSignatureImage(String userCN) {
		Image image = null;
		// Container base to search
		String searchBase = "DC=gaje,DC=us";
		String filter = "(&(objectClass=pkiUser)(cn=" + userCN +"))"; 

		LDAPConnection ldapConnection = new LDAPConnection();

		try {
			// Connect to the server
			ldapConnection.connect(settings.getHost(), settings.getPort());

			// bind to the server
			ldapConnection.bind(ldapVersion, settings.getLoginDn(), settings.getLoginPassword().getBytes("UTF8"));

			// perform search
			LDAPSearchResults searchResult = ldapConnection.search(searchBase, searchScope, filter, null, false);

			if (searchResult.hasMore()){
				LDAPEntry nextEntry = searchResult.next();
				LDAPAttribute userImageName = nextEntry.getAttribute("jpegPhoto");
				if (userImageName != null) {
					byte[] imageBytes = userImageName.getByteValue();
					try {
						image = Image.getInstance(imageBytes);
					} catch (BadElementException e) {
						e.printStackTrace();
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		} catch (LDAPException e) {
			log.error("Error: " + e.toString(),e);	
			if(e.getCause().toString().contains("Connection Refused"))
			{
				log.error("Can't connect to ldap");
			}
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			log.error("Error: " + e.toString(),e);
		}  
		return image;
	}
	public String retreivePwdForLDAP() {
		return "zObHPp9";
		//return "aditya";
	}

}
